package com.example.b_308.myapplication.view.user.login;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.b_308.myapplication.R;
import com.example.b_308.myapplication.controller.UserController;
import com.example.b_308.myapplication.model.Staff;
import com.example.b_308.myapplication.view.base.BaseActivity;
import com.example.b_308.myapplication.view.main.MainActivity;
import com.example.b_308.myapplication.view.user.register.RegisterActivity;

/**
 * Created by B-308 on 2017/11/15.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener, IUserView {
    EditText et_phone, et_password;
    ImageView iv_phone_delete, iv_password_delete;
    TextView tv_register, tv_forget_password;
    Button btn_login;
    UserController userController;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
        initListener();
    }

    private void initListener() {
        iv_phone_delete.setOnClickListener(this);
        iv_password_delete.setOnClickListener(this);
        tv_register.setOnClickListener(this);
        tv_forget_password.setOnClickListener(this);
        btn_login.setOnClickListener(this);
        et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length() == 0){
                    iv_phone_delete.setVisibility(View.GONE);
                }else{
                    iv_phone_delete.setVisibility(View.VISIBLE);
                }
            }
            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length() == 0){
                    iv_password_delete.setVisibility(View.GONE);
                }else{
                    iv_password_delete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


    }

    private void initData() {
        userController = new UserController(this);
    }
    private void initView() {
        setContentView(R.layout.activity_login);
        getTitleBar().setVisibility(View.GONE);
        et_phone = findView(R.id.et_phone);
        et_password = findView(R.id.et_password);
        iv_phone_delete = findView(R.id.iv_phone_delete);
        iv_password_delete = findView(R.id.iv_password_delete);
        btn_login = findView(R.id.btn_login);
        tv_register = findView(R.id.tv_register);
        tv_forget_password = findView(R.id.tv_forget_password);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_phone_delete:
                et_phone.setText("");
                break;
            case R.id.iv_password_delete:
                et_password.setText("");
                break;
            case R.id.btn_login:
                userController.login(et_phone.getText().toString(), et_password.getText().toString());
                break;
            case R.id.tv_register:
                tend2(RegisterActivity.class);
                break;
            case R.id.tv_forget_password:
                tend2(ForgetpwActivity.class);
                break;
        }
    }
    @Override
    public void loginSuccess(Staff staff) {
        tend2(MainActivity.class);
    }

    @Override
    public void registerSuccess() {

    }

    @Override
    public void getCodeSuccess() {

    }
}
